<?php
	function paging($totalPage, $currentPage, $minPage, $previousPage, $nextPage, $maxPage)
	{
	    $minPage = ($currentPage - 1 > 1 ? ($currentPage - 1) : 1);
	    $maxPage = ($currentPage + 1 < $totalPage ? ($currentPage + 1) : $totalPage);
	    $previousPage = $currentPage - 1;
	    $nextPage = $currentPage + 1;
	}
        function showPageAjax($totalPage, $page, $updateZone, $url)
	{
	    if ($totalPage > 1)
	    {
	        $minPage = 0;
	        $maxPage = 0;
	        $previousPage = 0;
	        $nextPage = 0;
	        paging($totalPage, $page, &$minPage, &$previousPage, &$nextPage, &$maxPage);

	        $urlToFirst = sprintf($url, 1);
	        $urlToPrevious = sprintf($url, $previousPage);
	        $urlToNext = sprintf($url, $nextPage);
	        $urlToLast = sprintf($url, $totalPage);	        
	        if ($minPage == 1)
	        {
	            $linkToFirst = '';
	        }
	        elseif ($minPage == 2)
	        {
	            $linkToFirst = link_to_remote(1, array('update' => array('success'=>$updateZone,'failure'=>'error'), 'url' => $urlToFirst, 'script'=>true,'loading'  => "showLoading('$updateZone')"), array ( 'class' => 'fcol-1'));
	        }
	        else
	        {
	            $linkToFirst = link_to_remote('<img border="0" src="/images/first_page.gif"/>', array('update' => array('success'=>$updateZone,'failure'=>'error'), 'url' => $urlToFirst,'script'=>true,'loading'  => "showLoading('$updateZone')"), array ('class' => 'fcol-1')).'<span class="dottrigger">...</span>';
	        }
	        $linkToNext = ($nextPage > $totalPage ? '' : link_to_remote('<img border="0" src="/images/next_page.gif"/>', array('update' => array('success'=>$updateZone,'failure'=>'error'), 'url' => $urlToNext, 'script'=>true,'loading'  => "showLoading('$updateZone')")));
                $linkToLast = link_to_remote('<img border="0" src="/images/last_page.gif"/>', array('update' => array('success'=>$updateZone,'failure'=>'error'), 'url' => $urlToLast, 'script'=>true,'loading'  => "showLoading('$updateZone')"));
	        $linkToPage = '';
                if ($maxPage >= $totalPage)
	        {
	            $linkToLast = '';
	        }
	        for ($i = $minPage; $i <= $maxPage; $i++)
	        {
	            $urlToPage = sprintf($url, $i);
	            $linkToPage.=($i == $page ?'<a style="color:red">'.$i.'</a>' : link_to_remote($i, array('update' => array('success'=>$updateZone,'failure'=>'error'), 'url' => $urlToPage, 'script'=>true,'loading'  => "showLoading('$updateZone')"), array ( 'class' => 'fcol-1')));
	        }
                if ($maxPage < $totalPage )
	        {       
	            $linkToPage.='<span class="dottrigger">... </span>';
	        }                
                echo  $linkToFirst.$linkToPage.$linkToNext.$linkToLast;
	    }
	}
        function showPage_1($totalPage, $page, $url)
	{
	    if ($totalPage > 1)
	    {
	        $minPage = 0;
	        $maxPage = 0;
	        $previousPage = 0;
	        $nextPage = 0;
	        paging($totalPage, $page, &$minPage, &$previousPage, &$nextPage, &$maxPage);

	        $urlToFirst = sprintf($url, 1);
	        $urlToPrevious = sprintf($url, $previousPage);
	        $urlToNext = sprintf($url, $nextPage);
	        $urlToLast = sprintf($url, $totalPage);

	        if ($minPage == 1)
	        {
	            $linkToFirst = '';
	        }
	        else
	        if ($minPage == 2)
	        {
	            $linkToFirst = '<a href="'.$urlToFirst.'">1</a>';//link_to(1, $urlToFirst);
	        }
	        else
	        {
	            $linkToFirst = '<a href="'.$urlToFirst.'">1</a>...';//link_to(1, $urlToFirst).'...';
	        }

	        //$linkToPrevious = ($previousPage < 1 ? '' : link_to(PAGE_PREVIOUS, $urlToPrevious));

	        if ($maxPage == $totalPage)
	        {
	            $linkToLast = '';
	        }
	        else
	        if ($maxPage == $totalPage - 1)
	        {
	            $linkToLast = '<a href="'.$urlToLast.'">'.$totalPage.'</a>';//link_to($totalPage, $urlToLast);
	        }
	        else
	        {
	            $linkToLast = '<li>...</li><li><a href="'.$urlToLast.'">'.$totalPage.'</a>';//link_to($totalPage, $urlToLast).'</li>';
	        }

	        //$linkToNext = ($nextPage > $totalPage ? '' : link_to(PAGE_NEXT, $urlToNext));
	        $linkToPage = '';
	        for ($i = $minPage; $i <= $maxPage; $i++)
	        {
	            $urlToPage = sprintf($url, $i);
	            $linkToPage.=($i == $page ?'<li><a class="page_current">'.$i.'</a></li>' :'<li><a href="'.$urlToPage.'">'.$i.'</a></li>'.'');
	        }
	        echo  '<div>Trang: '.
	        '
	                     <ul>
	                         <li>'.$linkToFirst.'</li>'.
	        $linkToPage.
	        '<li>'.$linkToLast.'</li>
	                     </ul>
	                   </div>
	               ';
	    }
	}
//	function showPage($totalPage, $page, $url)
//	{
//	    if ($totalPage > 1)
//	    {
//	        $minPage = 0;
//	        $maxPage = 0;
//	        $previousPage = 0;
//	        $nextPage = 0;
//	        paging($totalPage, $page, &$minPage, &$previousPage, &$nextPage, &$maxPage);
//
//	        $urlToFirst = sprintf($url, 1);
//	        $urlToPrevious = sprintf($url, $previousPage);
//	        $urlToNext = sprintf($url, $nextPage);
//	        $urlToLast = sprintf($url, $totalPage);
//
//	        if ($minPage == 1)
//	        {
//	            $linkToFirst = '';
//	        }
//	        else
//	        if ($minPage == 2)
//	        {
//	            $linkToFirst = link_to(1, $urlToFirst);
//	        }
//	        else
//	        {
//	            $linkToFirst = link_to(1, $urlToFirst).'<span class="dottrigger">...</span>';
//	        }
//
//	        //$linkToPrevious = ($previousPage < 1 ? '' : link_to(PAGE_PREVIOUS, $urlToPrevious));
//
//	        if ($maxPage == $totalPage)
//	        {
//	            $linkToLast = '';
//	        }
//	        else
//	        if ($maxPage == $totalPage - 1)
//	        {
//	            $linkToLast = link_to($totalPage, $urlToLast);
//	        }
//	        else
//	        {
//	            $linkToLast = '<li><span class="dottrigger">...</span></li><li>'.link_to($totalPage, $urlToLast).'</li>';
//	        }
//
//	        //$linkToNext = ($nextPage > $totalPage ? '' : link_to(PAGE_NEXT, $urlToNext));
//	        $linkToPage = '';
//	        for ($i = $minPage; $i <= $maxPage; $i++)
//	        {
//	            $urlToPage = sprintf($url, $i);
//	            $linkToPage.=($i == $page ?'<li><a class="page_current">'.$i.'</a></li>' :'<li>'.link_to($i, $urlToPage).'</li>'.'');
//	        }
//	        echo  '<div>Trang: '.
//	        '
//	                     <ul>
//	                         <li>'.$linkToFirst.'</li>'.
//	        $linkToPage.
//	        '<li>'.$linkToLast.'</li>
//	                     </ul>
//	                   </div>
//	               ';
//	    }
//	}
//
	
?>